package com.yonyou.jrmob.application.ui.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatDialog;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.yonyou.jrmob.application.R;

/**
 * Created by Marvin on 2017/4/12.
 */

public class CenterDialog extends AppCompatDialog implements View.OnClickListener {
    private Context context;      // 上下文
    private int layoutResID;      // 布局文件id
    private int[] listenedItems;  // 要监听的控件id

    public CenterDialog(Context context, int layoutResID, int[] listenedItems) {
        super(context, R.style.dialog_custom); //dialog的样式
        this.context = context;
        this.layoutResID = layoutResID;
        this.listenedItems = listenedItems;
    }

    public CenterDialog(Context context) {
        super(context);
    }

    public CenterDialog(Context context, int theme) {
        super(context, theme);
    }

    protected CenterDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private View rootView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        rootView = LayoutInflater.from(context).inflate(layoutResID, null);
        setContentView(rootView);

        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = display.getWidth() * 4 / 5; // 设置dialog宽度为屏幕的4/5
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失
        //遍历控件id,添加点击事件
        for (int id : listenedItems) {
            findViewById(id).setOnClickListener(this);
        }
    }
    public void startEnterAnim(){
//        anim.start();
//        anim1.start();
    }
    private OnCenterItemClickListener listener;

    public interface OnCenterItemClickListener {
        void OnCenterItemClick(CenterDialog dialog, View view);
    }

    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
        this.listener = listener;
    }

    public View getView(@IdRes int resId) {
        return rootView.findViewById(resId);
    }

    public void setDialogType(String type) {
        switch (type) {
            case "1":
                ((ImageView)rootView.findViewById(R.id.dialog_center_type)).setImageResource(R.drawable.dialog_top_notice);
                break;
            case "3":
                ((ImageView)rootView.findViewById(R.id.dialog_center_type)).setImageResource(R.drawable.dialog_top_alerm);
                break;
            case "2":
                ((ImageView)rootView.findViewById(R.id.dialog_center_type)).setImageResource(R.drawable.dialog_top_notice);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        dismiss();//注意：我在这里加了这句话，表示只要按任何一个控件的id,弹窗都会消失，不管是确定还是取消。
        if (listener != null) {
            listener.OnCenterItemClick(this, view);
        }
    }
}
